package org.dochandler.test.tester;

import static org.junit.Assert.assertEquals;

import org.dochandler.service.domain.Officer;
import org.dochandler.service.domain.constants.Constants;
import org.dochandler.service.domain.mission.Coordinate;
import org.dochandler.service.domain.mission.GlobalPosition;
import org.dochandler.service.domain.mission.Mission;
import org.dochandler.service.domain.mission.MissionReport;
import org.dochandler.test.service.MissionReportServiceTest;
import org.dochandler.test.service.core.MissionReportServiceTestImpl;
import org.esfinge.guardian.context.AuthorizationContext;
import org.esfinge.guardian.context.WrappedObj;
import org.esfinge.guardian.exception.AuthorizationException;
import org.junit.Before;
import org.junit.Test;

public class OfficerGlobalPositionTester {
	private Coordinate coordinate1;
	private Coordinate coordinate2;
	private Officer officer;
	
	@Before
	public void setUp() {
		officer = new Officer();
		coordinate1 = new Coordinate(0d, 0d, 0d);
		coordinate2 = new Coordinate(1d, 1d, 1d);
	}
	
	
	/**
	 * Test of the Security Policy Id SP03
	 */
	@Test
	public void missionReportingFromTheSameSiteTest() {
		GlobalPosition gp1 = new GlobalPosition(coordinate1, coordinate2);
		
		Mission mission = new Mission();
		mission.setLocation(gp1);
		
		officer.setGlobalPosition(gp1);
		
		WrappedObj<Mission> w1 = AuthorizationContext.wrapAsResourceProp(Constants.MISSION.val(), mission);
		WrappedObj<Officer> w2 = AuthorizationContext.wrapAsSubjectProp(Constants.OFFICER.val(), officer);
		
		MissionReport missionReport = new MissionReport();
		MissionReportServiceTest mrs = AuthorizationContext.guardObject(new MissionReportServiceTestImpl(), w1, w2);
		assertEquals("Officers in the same site of the mission should be able to report", new Boolean(true), mrs.aServiceToBeExecutedFromMissionSite(missionReport) );

	}
	
	
	/**
	 * Test of the Security Policy Id SP03
	 */
	@Test(expected=AuthorizationException.class)
	public void missionReportingFromADifferentSiteTest() {
		GlobalPosition gp1 = new GlobalPosition(coordinate1, coordinate2);
		Mission mission = new Mission();
		mission.setLocation(gp1);
		
		GlobalPosition gp2 = new GlobalPosition(coordinate2, coordinate1);
		officer.setGlobalPosition(gp2);
		
		WrappedObj<Mission> w1 = AuthorizationContext.wrapAsResourceProp(Constants.MISSION.val(), mission);
		WrappedObj<Officer> w2 = AuthorizationContext.wrapAsSubjectProp(Constants.OFFICER.val(), officer);
		
		MissionReport missionReport = new MissionReport();
		MissionReportServiceTest mrs = AuthorizationContext.guardObject(new MissionReportServiceTestImpl(), w1, w2);
		mrs.aServiceToBeExecutedFromMissionSite(missionReport);

	}
	
}
